//
//  ZJHShakeDetailViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "ZJHShakeDetailViewController.h"
#import "ZJHShakeDetailTableViewCell.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import "ZJHShakeModel.h"
#import "FontSizeSingleTon.h"
#define Font [[FontSizeSingleTon shareFontSize]array]
#import "ZJHShakeCaipuViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <MBProgressHUD.h>
#import "Loading.h"
@interface ZJHShakeDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataArray;
@property(nonatomic, strong)Loading *load;
@end

@implementation ZJHShakeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.title = @"摇一摇";
#pragma mark 设置摇动
    [[UIApplication sharedApplication]setApplicationSupportsShakeToEdit:YES];
    [self becomeFirstResponder];
   [self getData];
}
-(void)loadView
{
    [super loadView];
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 50 * ht)];
    headerView.backgroundColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(75 * wt, 10 * ht, 30 * wt, 30 * ht)];
    imageView.image = [UIImage imageNamed:@"iconfont-yaoyiyao"];
    [headerView addSubview:imageView];
    UILabel *headerLable = [[UILabel alloc]initWithFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width + 10 * wt, 15 * ht, 200 * wt, 20 * ht)];
    headerLable.text = @"没有找到复合口味的菜?摇一摇";
    headerLable.font = [UIFont systemFontOfSize:[Font[1] integerValue]];
    [headerView addSubview:headerLable];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableHeaderView = headerView;
    [self.view addSubview:_tableView];
    
}
-(void)createProgressView
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 0; i < 70; i++) {
        [array addObject:[NSString stringWithFormat:@"%ld", i + 1]];
    }
    _load = [[Loading alloc]initWithFrame:CGRectMake(0, 0, 375 * wt, 667 * ht - 64 - 49) PicArray:array];
    [self.view addSubview:_load];
}
-(void)getData
{
    [self createProgressView];
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    NSInteger date = time;
    _dataArray = [NSMutableArray array];
    NSString *post1 = @"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=e23f047aa9e310e54fdf26b09407ca6a&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7CCC2D6423-97F6-437A-B8DE-CBB86A71C6F1%7C608CBAFB-426B-4C02-96CA-F5B029C17D76&format=json&loguid=&method=Info.shake&nonce=1446621530&sessionid=1446621247&signmethod=md5&";
    NSString *post2 = [NSString stringWithFormat:@"timestamp=%ld&uuid=f54ccedd4a37e51b4340c5dc18fe353c&v=2&vc=43&vn=v5.4.0", date];
    NSString *postString = [NSString stringWithFormat:@"%@%@", post1, post2];
    NSString *bodyString = @"sign=&uid=&uuid=f54ccedd4a37e51b4340c5dc18fe353c";
    [HTTPMethod getDataByString:postString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        NSDictionary *allDic = [data objectForKey:@"result"];
        NSArray *listArray = [allDic objectForKey:@"list"];
        for (NSDictionary *dic in listArray) {
            ZJHShakeModel *model = [[ZJHShakeModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:model];
        }
        
        [_tableView reloadData];
        [_load removeFromSuperview];
    }];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    ZJHShakeDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ZJHShakeDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count != 0) {
        cell.shakeModel = _dataArray[indexPath.row];
    }
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90 * ht;
}
#pragma mark cell点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZJHShakeCaipuViewController *vc = [[ZJHShakeCaipuViewController alloc]init];
    ZJHShakeModel *shakeModel = _dataArray[indexPath.row];
    vc.tempModel = shakeModel;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark 摇一摇震动
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"shake"]) {
        
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
    
}
#pragma mark 摇动结束
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    [self getData];
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
